package com.centit.cxtj.service.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.UnknownHostException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.support.database.utils.PageDesc;
import com.centit.util.ZipUtil;
import com.centit.framework.jdbc.service.BaseEntityManagerImpl;
import com.centit.airplanebaseinfo.dao.AirplaneBaseinfoDao;
import com.centit.airplanebaseinfo.po.AirplaneBaseinfo;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.centit.airplanebaseinfo.service.AirplaneBaseinfoManager;
import com.centit.cxtj.dao.Cxtjdao;
import com.centit.cxtj.service.CxtjManager;
import com.centit.exchange.dao.ExchangeRateDao;
import com.centit.exchange.po.ExchangeRate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * AirplaneBaseinfo  Service.
 * create by scaffold 2020-03-13 
 * @author codefan@sina.com
 * 	查询统计 service
*/
@Service
public class CxtjManagerImpl 
		extends BaseEntityManagerImpl<AirplaneBaseinfo,java.lang.String,Cxtjdao>
	implements CxtjManager{

	public static final Log log = LogFactory.getLog(AirplaneBaseinfoManager.class);

	
	private Cxtjdao cxtjdao;
	@Autowired
	private AirplaneBaseinfoDao airplaneBaseinfoDao;
	@Autowired
	private ExchangeRateDao exchangeRateDao;
	
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
	
	SimpleDateFormat dateSdf = new SimpleDateFormat("yyyy-MM-dd");
	
	private String[] stringArray = new String[] {"dtz","shz","gjz","ht","pw"};
	
	@Resource(name = "cxtjdao")
    @NotNull
	public void setCxtjdao(Cxtjdao baseDao)
	{
		this.cxtjdao = baseDao;
		setBaseDao(this.cxtjdao);
	}
	
	
	
	@Override
	public JSONArray listAirplaneBaseinfosAsJson(String[] fields, Map<String, Object> filterMap, PageDesc pageDesc) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public JSONArray getFjyjqlList(Map<String, Object> filterMap, PageDesc pageDesc) {
		if(filterMap.containsKey("nf") && StringUtils.isNotBlank(filterMap.get("nf").toString())) {
			String nf = filterMap.get("nf").toString();
			if(nf.equals("1")) {
				filterMap.put("nf", 5);
			} else if (nf.equals("2")) {
				filterMap.put("nf", 10);
			} else if (nf.equals("3")) {
				filterMap.put("nf", 15);
			} else {
				filterMap.remove("nf");
			} 
		}
		JSONArray jsonArray = cxtjdao.getFjyjqlList(filterMap, pageDesc);
		int index = 1;
		for(int i = 0 ; i<jsonArray.size() ; i++ ) {
			JSONObject obj = jsonArray.getJSONObject(i);
			if(i == 0) {
				obj.put("xh", index);
			} else if(obj.getString("nf").equals(jsonArray.getJSONObject(i-1).getString("nf"))) {
				index++;
				obj.put("xh", index);
			} else {
				index = 1;
				obj.put("xh", index);
			}
		}
		return jsonArray;
	}

	

	@Override
	public JSONArray getJlfxList(Map<String, Object> filterMap) {
		// TODO Auto-generated method stub
		return cxtjdao.getJlfxList(filterMap);
	}

	@Override
	public JSONArray getFjzlxzList(Map<String, Object> filterMap) {
		// TODO Auto-generated method stub
		return cxtjdao.getFjzlxzList(filterMap);
	}

	@Override
	public JSONArray homePageTopNum(Map<String, Object> filterMap) {
		// TODO Auto-generated method stub
		return cxtjdao.homePageTopNum(filterMap);
	}
	
	/**
	 * 	历年机队信息表
	 */
	@Override
	public JSONArray lnjdxxb(Map<String, Object> filterMap) {
		if(filterMap.containsKey("nf") && StringUtils.isNotBlank(filterMap.get("nf").toString())) {
			String nf = filterMap.get("nf").toString();
			if(nf.equals("1")) {
				filterMap.put("qj", 5);
			} else if (nf.equals("2")) {
				filterMap.put("qj", 10);
			} else if (nf.equals("3")) {
				filterMap.put("qj", 15);
			} 
		}
		JSONArray jxlist = cxtjdao.jxList();
		JSONArray nflist = cxtjdao.nfList(filterMap);
		List<AirplaneBaseinfo> listObjects = airplaneBaseinfoDao.listObjects();
		for (int i = 0; i < nflist.size(); i++) {
			JSONObject obj = nflist.getJSONObject(i);
			int allNum = 0;
			for (int j = 0; j < jxlist.size(); j++) {
				JSONObject objj = jxlist.getJSONObject(j);
				int num = 0;
				for (AirplaneBaseinfo airplaneBaseinfo : listObjects) {
					if(airplaneBaseinfo.getAirModel()==null||StringUtils.isBlank(airplaneBaseinfo.getAirModel())){
						continue;
					}
					if(airplaneBaseinfo.getAirModel().equals(objj.getString("airModel")) && airplaneBaseinfo.getDtbfDate() != null && airplaneBaseinfo.getDtyxDate() != null &&
							Integer.valueOf(sdf.format(airplaneBaseinfo.getDtbfDate())) <= obj.getIntValue("nf") &&
							Integer.valueOf(sdf.format(airplaneBaseinfo.getDtyxDate())) >= obj.getIntValue("nf")) {
						num ++;
					}
				}
				obj.put(objj.getString("airModel"), num);
				allNum += num;
			}
			obj.put("allNum", allNum);
		}
		return nflist;
	}



	@Override
	public JSONArray jxlist() {
		// TODO Auto-generated method stub
		return cxtjdao.jxList();
	}



	@Override
	public JSONArray fjxxqk(Map<String, Object> filterMap) {
		if(filterMap.containsKey("nf") && StringUtils.isNotBlank(filterMap.get("nf").toString())) {
			String nf = filterMap.get("nf").toString();
			if(nf.equals("1")) {
				filterMap.put("nf", 5);
			} else if (nf.equals("2")) {
				filterMap.put("nf", 10);
			} else if (nf.equals("3")) {
				filterMap.put("nf", 15);
			} else {
				filterMap.remove("nf");
			} 
		}
		return cxtjdao.fjxxqk(filterMap);
	}



	@Override
	public JSONArray fjxxqkhj(Map<String, Object> filterMap) {
		if(filterMap.containsKey("nf") && StringUtils.isNotBlank(filterMap.get("nf").toString())) {
			String nf = filterMap.get("nf").toString();
			if(nf.equals("1")) {
				filterMap.put("nf", 5);
			} else if (nf.equals("2")) {
				filterMap.put("nf", 10);
			} else if (nf.equals("3")) {
				filterMap.put("nf", 15);
			} else {
				filterMap.remove("nf");
			} 
		}
		return cxtjdao.fjxxqkhj(filterMap);
	}



	@Override
	public JSONArray fjyjfx(Map<String, Object> filterMap) {
		if(filterMap.containsKey("nf") && StringUtils.isNotBlank(filterMap.get("nf").toString())) {
			String nf = filterMap.get("nf").toString();
			if(nf.equals("1")) {
				filterMap.put("qj", 5);
			} else if (nf.equals("2")) {
				filterMap.put("qj", 10);
			} else if (nf.equals("3")) {
				filterMap.put("qj", 15);
			} 
		}
		JSONArray nflist = cxtjdao.nfList(filterMap);
		JSONArray dataArray = cxtjdao.fjyjfx(filterMap);
		for (int i = 0; i < nflist.size(); i++) {
			JSONObject obj = nflist.getJSONObject(i);
			for (int j = 0; j < dataArray.size(); j++) {
				JSONObject objj = dataArray.getJSONObject(j);
				if(objj.getIntValue("nf") ==obj.getIntValue("nf")) {
					if(objj.getIntValue("type") == 0) {
						obj.put("tc", objj.getIntValue("num")*-1);
					} else if(objj.getIntValue("type") == 1) {
						obj.put("yj", objj.getIntValue("num"));
					}
				}
			}
		}
		return nflist;
	}



	@Override
	public JSONArray fjsjcsgl(Map<String, Object> filterMap) {
		if(filterMap.containsKey("starnf") && StringUtils.isNotBlank(filterMap.get("starnf").toString()) && filterMap.containsKey("endnf") && StringUtils.isNotBlank(filterMap.get("endnf").toString())) {
			Date ksdata = null;
			Date jsdata = null;
			try {
				ksdata = dateSdf.parse(filterMap.get("starnf").toString());
				jsdata = dateSdf.parse(filterMap.get("endnf").toString());
			} catch (ParseException e) {
				e.printStackTrace();
			};
			JSONArray listObjectsAsJson = cxtjdao.listObjectsAsJson(filterMap, null);
			DecimalFormat df = new DecimalFormat("###,###.##"); 
			df.setMaximumFractionDigits(2);//这里是小数位   
			for (int i = 0; i < listObjectsAsJson.size(); i++) {
				JSONObject object = listObjectsAsJson.getJSONObject(i);
				int zjbz = object.getIntValue("zjbz");
				if(zjbz == 1) {  //人民币
					float htje =  object.getFloatValue("mqzj");
					object.put("htMoney", "¥"+df.format(object.getFloatValue("mqzj")));
					ExchangeRate qExchangeRate = exchangeRateDao.getObjectById(filterMap.get("starnf").toString());
					if(qExchangeRate != null) {
						double d = htje/(Double.parseDouble(qExchangeRate.getBankConversionPri())/100);
						object.put("qhl", "$"+df.format(d));
					}
					ExchangeRate hExchangeRate = exchangeRateDao.getObjectById(filterMap.get("endnf").toString());
					if(hExchangeRate != null) {
						double d = htje/(Double.parseDouble(hExchangeRate.getBankConversionPri())/100);
						object.put("hhl","$"+df.format(d));
					}
				} else if(zjbz == 2) {  //美元
					float htje =  object.getFloatValue("mqzj");
					object.put("htMoney", "$"+df.format(object.getFloatValue("mqzj")));
					ExchangeRate qExchangeRate = exchangeRateDao.getObjectById(filterMap.get("starnf").toString());
					if(qExchangeRate != null) {
						double d = htje*(Double.parseDouble(qExchangeRate.getBankConversionPri())/100);
						object.put("qhl", "¥"+df.format(d));
					}
					ExchangeRate hExchangeRate = exchangeRateDao.getObjectById(filterMap.get("endnf").toString());
					if(hExchangeRate != null) {
						double d = htje*(Double.parseDouble(hExchangeRate.getBankConversionPri())/100);
						object.put("hhl","¥"+df.format(d));
					}
				}
			}
			return listObjectsAsJson;
		} else {
			return new JSONArray();
		}
		
	}



	@Override
	public JSONArray listAirPlain(Map<String, Object> filterMap) {
		JSONArray jsonArray = cxtjdao.listAirPlain(filterMap);
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			Set<String> keys = jsonObject.keySet();
			for (String key : keys) {
				try {
					if(key.contains("Date") || key.contains("Time") || key.contains("tcygrq") || key.contains("jrygrq")) {
						jsonObject.put(key, dateSdf.format(jsonObject.getDate(key)));
					}
				} catch (Exception e) {
					continue;
				}
			}
		}
		return jsonArray;
	}



	@Override
	public void downAirListFj(JSONArray list, JSONArray jsonArrayObjects,HttpServletResponse response) {
		List<String> tempList = Arrays.asList(stringArray);
		JSONArray jsonArray = new JSONArray();
		for(int i = 0; i< list.size(); i++) {
			JSONObject jsonObject = list.getJSONObject(i);
			if(tempList.contains(jsonObject.getString("value"))){
				jsonArray.add(jsonObject);
			} 
		}
		try {
			ZipUtil.creartDir(jsonArrayObjects, jsonArray,response);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
}

